<?php

namespace App\Http\Controllers\Api\Common;

use App\Http\Controllers\Controller;
use App\Http\Requests\Common\EpidemicPolicyRequest;
use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use App\Trip\Epidemic\Juhe\Aggregation;
use App\Trip\Train\TrainQueryFromToData;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;

class EpidemicController extends Controller
{

    private AreaRepositoryInterface $areaRepo;

    public function __construct(AreaRepositoryInterface $areaRepository)
    {
        $this->areaRepo = $areaRepository;
    }

    /**
     * 防疫政策
     *
     * @throws GuzzleException
     * @throws Exception
     */
    public function policy(EpidemicPolicyRequest $request): Response|Application|ResponseFactory
    {
        $e = new Aggregation();
        $e->setLanguage($request->header('language', App::getLocale()) ?: App::getLocale());
        $fromData = new TrainQueryFromToData($request->input('from'));
        $toData   = new TrainQueryFromToData($request->input('to'));
        $from     = $this->areaRepo->findByCode($fromData->parse2City()['code']);
        $to       = $this->areaRepo->findByCode($toData->parse2City()['code']);
        if (!$from || !$to) {
            return $this->error(0, "暂无该城市数据", []);
        }
        return $this->success('', $e->policy($from, $to));
    }
}
